package com.miniapp.core.service.impl;


import com.miniapp.common.utils.DateUtils;
import com.miniapp.common.utils.SecurityUtils;
import com.miniapp.core.domain.PaicFtiAgentGen;
import com.miniapp.core.mapper.PaicFtiAgentGenMapper;
import com.miniapp.core.service.IPaicFtiAgentGenService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 平安货运险代理人Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-29
 */
@Service
public class PaicFtiAgentGenServiceImpl implements IPaicFtiAgentGenService
{
    @Autowired
    private PaicFtiAgentGenMapper paicFtiAgentGenMapper;



    /**
     * 查询平安货运险代理人
     *
     * @param id 平安货运险代理人主键
     * @return 平安货运险代理人
     */
    @Override
    public PaicFtiAgentGen selectPaicFtiAgentGenById(Long id)
    {
        return paicFtiAgentGenMapper.selectPaicFtiAgentGenById(id);
    }

    /**
     * 查询平安货运险代理人列表
     *
     * @param paicFtiAgentGen 平安货运险代理人
     * @return 平安货运险代理人
     */
    @Override
    public List<PaicFtiAgentGen> selectPaicFtiAgentGenList(PaicFtiAgentGen paicFtiAgentGen)
    {
        return paicFtiAgentGenMapper.selectPaicFtiAgentGenList(paicFtiAgentGen);
    }

    /**
     * 新增平安货运险代理人
     *
     * @param paicFtiAgentGen 平安货运险代理人
     * @return 结果
     */
    @Override
    public int insertPaicFtiAgentGen(PaicFtiAgentGen paicFtiAgentGen)
    {
        String agentPhone = paicFtiAgentGen.getAgentPhone();

        PaicFtiAgentGen paicFtiAgentGenQuery = new PaicFtiAgentGen();
        paicFtiAgentGenQuery.setAgentPhone(agentPhone);
        List<PaicFtiAgentGen> paicFtiAgentGenList = paicFtiAgentGenMapper.selectPaicFtiAgentGenList(paicFtiAgentGenQuery);
        if(CollectionUtils.isNotEmpty(paicFtiAgentGenList)){
            throw new RuntimeException("手机号已存在，不允许提交");
        }
        paicFtiAgentGen.setCreateTime(DateUtils.getNowDate());
        paicFtiAgentGen.setUpdateTime(DateUtils.getNowDate());
        paicFtiAgentGen.setUserId(SecurityUtils.getUserId());
        return paicFtiAgentGenMapper.insertPaicFtiAgentGen(paicFtiAgentGen);
    }

    /**
     * 修改平安货运险代理人
     *
     * @param paicFtiAgentGen 平安货运险代理人
     * @return 结果
     */
    @Override
    public int updatePaicFtiAgentGen(PaicFtiAgentGen paicFtiAgentGen)
    {
        Long id = paicFtiAgentGen.getId();
        PaicFtiAgentGen oldPaicFtiAgent = paicFtiAgentGenMapper.selectPaicFtiAgentGenById(id);
        String oldAgentPhone = oldPaicFtiAgent.getAgentPhone();

        String agentPhone = paicFtiAgentGen.getAgentPhone();

        if(!oldAgentPhone.equals(agentPhone)){
            PaicFtiAgentGen queryPaicFtiAgentGen = new PaicFtiAgentGen();
            queryPaicFtiAgentGen.setAgentPhone(agentPhone);
            List<PaicFtiAgentGen> paicFtiAgentGenList = paicFtiAgentGenMapper.selectPaicFtiAgentGenList(paicFtiAgentGen);
            if(CollectionUtils.isNotEmpty(paicFtiAgentGenList)){
                throw new RuntimeException("手机号已存在");
            }


        }



        paicFtiAgentGen.setCreateTime(DateUtils.getNowDate());
        paicFtiAgentGen.setUpdateTime(DateUtils.getNowDate());
        paicFtiAgentGen.setUserId(SecurityUtils.getUserId());
        return paicFtiAgentGenMapper.updatePaicFtiAgentGen(paicFtiAgentGen);
    }

    /**
     * 批量删除平安货运险代理人
     *
     * @param ids 需要删除的平安货运险代理人主键
     * @return 结果
     */
    @Override
    public int deletePaicFtiAgentGenByIds(Long[] ids)
    {
        return paicFtiAgentGenMapper.deletePaicFtiAgentGenByIds(ids);
    }

    /**
     * 删除平安货运险代理人信息
     *
     * @param id 平安货运险代理人主键
     * @return 结果
     */
    @Override
    public int deletePaicFtiAgentGenById(Long id)
    {
        return paicFtiAgentGenMapper.deletePaicFtiAgentGenById(id);
    }
}
